<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 对象复制 深拷贝
        let obj = {
            title: '人员信息',
            user: {
                name: '张三',
                age: 20
            },
            hehe: [1, 2]
        }
        // 深拷贝
        let obj1 = JSON.parse(JSON.stringify(obj));
        console.log(obj);
        obj1.title = '人员信息修改';
        obj1.user.name = '李思';
        obj1.hehe[0] = '李思';
        obj1.hehe.push(123);
        console.log(obj1)
        // 深拷贝
        function copy(obj) {
            let res = obj instanceof Array ? [] : {};
            for (const [key, value] of Object.entries(obj)) {
                res[key] = typeof value == 'object' ? copy(value) : value;
            }
            return res;
        }
        const zh = copy(obj);
        zh.title = 'admin';
        zh.user.name = 'xiaoqiang';
        console.log(obj);
        console.log(zh);
        // 工厂函数创建对象
        function newUser(name, age) {
            return {
                name,
                age,
                show() {
                    console.log(this.name + '-zh.com');
                },
                info() {
                    console.log(this.name + '-' + this.age);
                }
            }
        };
        let user1 = newUser('张三', 18);
        let user2 = newUser('李思', 20);
        user1.show();
        user2.show();
    </script>
</body>

</html>